import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:juming_test/model/home/home_kingkong_model.dart';
import 'package:juming_test/model/home/kingkong_management_section_model.dart';
import 'package:juming_test/module/home/kingkong_management/widgets/kingkong_management_item.dart';

class KingkongManagementInactiveSections extends StatelessWidget {
  const KingkongManagementInactiveSections({
    super.key,
    required this.kingkongSections,
    required this.isEditing,
    required this.onAdd,
  });

  // 金刚位数据
  final List<KingkongManagementSectionModel> kingkongSections;
  // 编辑模式
  final bool isEditing;
  // 事件处理
  // 添加item
  final Function(HomeKingkongModel) onAdd;

  @override
  Widget build(BuildContext context) {
    return SliverList(
      delegate: SliverChildBuilderDelegate((context, sectionIndex) {
        return Padding(
          padding: EdgeInsets.all(16),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                kingkongSections[sectionIndex].title,
                style: TextStyle(fontSize: 16.sp, fontWeight: FontWeight.bold),
              ),
              SizedBox(height: 12),
              _buildModuleGrid(kingkongSections[sectionIndex].items, isEditing),
            ],
          ),
        );
      }, childCount: kingkongSections.length),
    );
  }

  // 单个模块的网格布局
  Widget _buildModuleGrid(List<HomeKingkongModel> items, bool isEditing) {
    return GridView.builder(
      shrinkWrap: true,
      physics: NeverScrollableScrollPhysics(),
      gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 4,
        mainAxisSpacing: 8,
        crossAxisSpacing: 8,
        childAspectRatio: 1,
      ),
      itemBuilder: (context, index) {
        return HomeKingkongManagementItem(
          isActive: false,
          kingkong: items[index],
          isEditing: isEditing,
          onAdd: (kingkong) => onAdd(kingkong),
        );
      },
      itemCount: items.length,
    );
  }
}
